<?php
namespace app\api\controller;
use app\api\common\Common;
use app\api\common\ApiErrDesc;

class Address extends Common{
    /**
     * 获取用户地址数据接口
     * @return   [Json]    [返回查询结果]
     */
    public function get_address(){
        $data = $this->params;
        $adds_list=db('address')->where('uid='.$data['uid'])->order('is_default desc,id desc')->select();
        // 查出来的数据只有省市区的code，需要进一步查询得到详细的中文地名
        foreach($adds_list as $k => $v){
            $text_provence = db('china_city')->where('id='.$v['provence'])->column('name');
            $adds_list[$k]['provence']= $text_provence[0];
            $text_city = db('china_city')->where('id='.$v['city'])->column('name');
            $adds_list[$k]['city'] = $text_city[0];
            $text_zone = db('china_city')->where('id='.$v['zone'])->column('name');
            $adds_list[$k]['zone'] = $text_zone[0];
            
            // 加密手机号码、去除uid
            $adds_list[$k]['phone']  = $this->encrypt_info($v['phone']);
            unset($adds_list[$k]['uid']);
        }

        if ($adds_list) {
			$this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], $adds_list);
		}else{
			$this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
		}
    }

    /**
     * 用户添加地址信息
     * @return  [JSON]    [返回新增地址信息]
     */
    public function add_address(){
        $data = $this->params;
        unset($data['token']);
        $check_id = db('address')->where($data)->column('id');
        if ($check_id) {
            $this->return_msg(ApiErrDesc::ADDNOTEXIST[0], ApiErrDesc::ADDNOTEXIST[1]);
        }
        $province_name = db('china_city')->where('id='.$data['provence'])->column('name');
        $city_name = db('china_city')->where('id='.$data['city'])->column('name');
        $zone_name = db('china_city')->where('id='.$data['zone'])->column('name');
        $data['address_xq'] = $province_name[0].' '.$city_name[0].' '.$zone_name[0].' '.$data['address'];
        $res = db('address')->insert($data);
        if ($res) {
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
        }else{
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }

    /**
     * 用户删除地址信息[可多个删除]
     * @return  [JOSN]    [删除结果]
     */
    public function del_address(){
        $data = $this->params;
        $res = db('address')->where('uid='.$data['uid'].' AND id IN ('.implode(",", $data['id_arr']).')')->delete();
        if ($res) {
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
        }else{
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }

    /**
     * 获取省份数据
     * @return  [JOSN]    [返回省份结果]
     */
    public function get_provence(){
        $list = db("china_city")->where('tid=0')->field('id,name')->select();
        if($list) {
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], $list);
        }else{
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }
    
    /**
     * 获取城市数据
     * @return  [JOSN]    [返回城市结果]
     */
    public function get_city(){
        $data = $this->params;
        $city = db("china_city")->where('tid='.$data['provenceid'])->field('id,name')->select();
        if($city) {
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], $city);
        }else{
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }

    /**
     * 获取区域数据与邮编code
     * @return  [JOSN]    [返回区域结果]
     */
    public function get_zone(){
        $data = $this->params;
        $area = db("china_city")->where('tid='.$data['cityid'])->field('id,name,code')->select();
        if($area) {
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], $area);
        }else{
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }

    /**
     * 设置默认地址
     * @return  [JOSN]    [返回设置结果]
     */
    public function set_default(){
        $data = $this->params;
        $check = db('address')->where('uid='.$data['uid'].' AND is_default=1')->find();
        if ($check) {
            $up1= db('address')->where('id='.$check['id'])->update(array('is_default'=>0));
            if (!$up1) {
                $this->return_msg(ApiErrDesc::ADDMODIFYERR[0], ApiErrDesc::ADDMODIFYERR[1]);
            }
        }
        
        $up2 = db('address')->where('id='.$data['addr_id'].' AND uid='.$data['uid'])->update(array('is_default'=>1));
        if($up2) {
            $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
        }else{
            $this->return_msg(ApiErrDesc::ERROR[0], ApiErrDesc::ERROR[1]);
        }
    }

}